<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/26
 * Time: 11:21
 */

class QuestionModel
{
    public function excellentAction($fid,$page,$number=PAGE_NUMBER)
    {
        $data['top'] = Db::name('ranking')
            ->field('sum(r.total) as total,sum(r.correct) as correct,ud.nickname,ud.section,ud.avatar')->alias('r')
            ->join('users_detail ud','r.udid = ud.id')
            ->where(['r.fid'=>$fid])
            ->group('r.udid')
            ->page($page,$number)
            ->order('correct desc , correct/total desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('ranking')->where(['fid'=>$fid])->count();
        return $data;
    }

    public function myRankingAction($data)
    {
        $Ranking = Db::name('ranking')
            ->field('sum(r.total) as total,sum(r.correct) as correct,count(r.llid) as number,ud.nickname,ud.section,ud.avatar')->alias('r')
            ->join('users_detail ud','r.udid = ud.id')
            ->where(['r.fid'=>$data['fid'],'r.udid'=>$data['id']])
            ->group('r.udid')
            ->order('correct desc , total desc')
            ->find();
        $user = new UserModel();
        $Ranking['ranking'] = $user->rankingAction($data,$data['id']);
        $Ranking['record'] = Db::name('ranking')
            ->field('ll.id,ll.name')->alias('r')
            ->join('live_list ll','r.llid = ll.id')
            ->where(['r.fid'=>$data['fid'],'r.udid'=>$data['id']])
            ->group('r.llid')
            ->order('correct desc , total desc')
            ->select();
        return $Ranking;
    }

    public function resultsAction($llid,$user)
    {
        $data['lists'] = Db::name('answer')
            ->field('q.topic,q.option_a,q.option_b,q.option_c,q.option_d,q.type,a.q_correct,a.m_correct')->alias('a')
            ->join('question q','q.id = a.qid')
            ->where(['a.udid'=>$user['id'],'a.fid'=>$user['fid'],'a.llid'=>$llid])
            ->select();
        $name = Db::name('live_list')
            ->field('name')
            ->where(['id'=>$llid])
            ->find();
        $number = Db::name('ranking')
            ->field('count(llid) as number')
            ->where(['fid'=>$user['fid'],'llid'=>$llid])
            ->find();
        $info = Db::name('ranking')
            ->field('total,correct,udid,createtime')
            ->where(['llid'=>$llid])
            ->group('udid')
            ->order('correct desc , correct/total desc')
            ->select();
        foreach ($info as $k=>$v) {
            if($v['udid'] == $user['id']){
                $data['ranking'] = $k+1;
                $data['total'] =$v['total'];
                $data['correct'] =$v['correct'];
                $data['createtime'] =$v['createtime'];
                break;
            }
        }
        $data['name'] = $name['name'];
        $data['number'] =$number['number'];
        return $data;
    }

    public function answerAction($llid,$user)
    {
        $data['lists'] = Db::name('question')
            ->field('id as qid,topic,option_a,option_b,option_c,option_d,type')
            ->where(['fid'=>$user['fid'],'llid'=>$llid,'status'=>1])
            ->order('id desc')
            ->select();
        $name = Db::name('live_list')
            ->field('name')
            ->where(['id'=>$llid])
            ->find();
        $data['name'] = $name['name'];
        return $data;
    }

    public function answerPostAction($content,$user)
    {
        $result = json_decode($content['result'],true);
        $correct=0;
        foreach ($result as $k=>$v){
            $question = Db::name('question')->field('id ,correct')->where(['id'=>$v['qid']])->find();
            $data[$k]['qid'] = $question['id'];
            $data[$k]['udid'] = $user['id'];
            $data[$k]['firm'] = $user['firm'];
            $data[$k]['fid'] = $user['fid'];
            $data[$k]['llid'] = $content['llid'];
            $data[$k]['q_correct'] = $question['correct'];
            $data[$k]['m_correct'] = $v['correct'];
            if($question['correct'] == $v['correct']){
                $correct++;
            }
        }
        if(!empty($data)){
            Db::name('answer')->insertAll($data);
            $ranking['udid'] = $user['id'];
            $ranking['firm'] = $user['firm'];
            $ranking['fid'] = $user['fid'];
            $ranking['llid'] = $content['llid'];
            $ranking['total'] = count($result);
            $ranking['correct'] = $correct;
            Db::name('ranking')->insert($ranking);
        }
        $msg['message'] = '试卷提交成功';
        return $msg;
    }

    public function addPostAction($llid,$content,$user)
    {
        $data = [];
        foreach ($content as $k=>$v){
            $data[$k]['udid']=$user['id'];
            $data[$k]['firm']=$user['firm'];
            $data[$k]['fid'] = $user['fid'];
            $data[$k]['llid']=$llid;
            $data[$k]['topic']=$v['topic'];
            $data[$k]['option_a']=$v['option_a'];
            $data[$k]['option_b']=$v['option_b'];
            $data[$k]['option_c']=$v['option_c'];
            $data[$k]['option_d']=$v['option_d'];
            $data[$k]['correct']=$v['correct'];
            $data[$k]['type']=$v['type'];
            $data[$k]['status']=1;
        }
        if(!empty($data)){
            Db::name('question')->insertAll($data);
            $data['message'] = '试题添加成功并已启用';
        }else{
            $data['message'] = '你未提交试题';
        }
        return $data;
    }

    public function editAction($llid,$qid,$user)
    {
        $data = Db::name('question')
            ->field('id as qid,llid,topic,option_a,option_b,option_c,option_d,type,correct')
            ->where(['fid'=>$user['fid'],'udid'=>$user['id'],'llid'=>$llid,'id'=>$qid])
            ->find();
        return $data;
    }

    public function editPostAction($content,$user)
    {
        $data['topic']=$content['topic'];
        $data['option_a']=$content['option_a'];
        $data['option_b']=$content['option_b'];
        $data['option_c']=$content['option_c'];
        $data['option_d']=$content['option_d'];
        $data['correct']=$content['correct'];
        $data['type']=$content['type'];
        Db::name('question')
            ->where(['id'=>$content['qid'],'fid'=>$user['fid'],'udid'=>$user['id'],'llid'=>$content['llid'],'status'=>1])
            ->update($data);
        $msg['message'] = '题目修改成功';
        return $msg;
    }

    public function delAction($content,$user)
    {
        $where['id']=$content['qid'];
        $where['udid']=$user['id'];
        $where['fid']=$user['fid'];
        $where['llid']=$content['llid'];
        $where['type']=$content['type'];
        $where['status']=1;
        Db::name('question')
            ->where($where)
            ->update(['status'=>0]);
        $msg['message'] = '题目删除成功';
        return $msg;
    }

    public function previewAction($llid,$user)
    {
        $data = Db::name('question')
            ->field('id as qid,llid,topic,option_a,option_b,option_c,option_d,type,correct')
            ->where(['fid'=>$user['fid'],'udid'=>$user['id'],'llid'=>$llid,'status'=>1])
            ->order('id asc')
            ->select();
        return $data;
    }
}